package com.explorx.sns.puma.im.core.controller;

import com.explorex.puma.common.HttpErrorCodeEum;
import com.explorex.puma.common.login.UserContextHolder;
import com.explorex.puma.common.web.HttpResult;
import com.explorx.sns.puma.im.core.chat.GroupManager;
import com.explorx.sns.puma.im.core.chat.consts.GroupMemberRoleEnum;
import com.explorx.sns.puma.im.core.chat.model.*;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.registry.nacos.NacosRegistry;
import org.apache.dubbo.spring.boot.context.event.WelcomeLogoApplicationListener;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/api/chat")
public class GroupController {

    @Resource
    private GroupManager  groupManager;

    @PostMapping("/v1/group/create")
    public HttpResult createGroup(@RequestBody CreateGroupDto createGroupDto){
        try {
            Preconditions.checkArgument(Objects.nonNull(createGroupDto.getCreatorId()),"用户id为空");
            Preconditions.checkArgument(createGroupDto.getCreatorId()>0,"用户id为空");
            Preconditions.checkArgument(StringUtils.isNoneBlank(createGroupDto.getGroupName()));
            Preconditions.checkArgument(StringUtils.isNoneBlank(createGroupDto.getGroupDesc()));
            final UserConvBase userConvBase = groupManager.createGroup(createGroupDto);
            return HttpResult.success(userConvBase);
        }catch (IllegalArgumentException e){
            log.error("group/create createGroupDto=req={}",createGroupDto,e);
            HttpResult.fail(HttpErrorCodeEum.ERROR_ILLEGAL_ARGUMENT);
        }catch (Exception e){
            log.error("group/create exception req={}",createGroupDto,e);
        }
        return HttpResult.fail(HttpErrorCodeEum.ERROR);
    }

    @PostMapping("/v1/group/join")
    public HttpResult joinGroup(@RequestBody  JoinGroupDto joinGroupDto){
        try {
            Preconditions.checkArgument(Objects.nonNull(joinGroupDto.getGroupId()));
            Preconditions.checkArgument(Objects.nonNull(joinGroupDto.getUserId()));
            groupManager.joinGroup(joinGroupDto, GroupMemberRoleEnum.MEMBER );
        }catch (IllegalArgumentException e){
            log.error("group/join joinGroupDto=req={}",joinGroupDto,e);
          return   HttpResult.fail(HttpErrorCodeEum.ERROR_ILLEGAL_ARGUMENT);
        }catch (Exception e){
            log.error("group/join exception req={}",joinGroupDto,e);
            return HttpResult.fail(-2,e.getMessage());
        }
        return HttpResult.success();
    }

    @PostMapping("/v1/group/leave")
    public HttpResult leaveGroup(@RequestBody LeaveGroupDto leaveGroupDto){
        try {
            Preconditions.checkArgument(Objects.nonNull(leaveGroupDto.getGroupId()));
            Preconditions.checkArgument(Objects.nonNull(leaveGroupDto.getUserId()));
            groupManager.leaveGroup(leaveGroupDto);
        }catch (IllegalArgumentException e){
            log.error("group/leave leaveGroupDto=req={}",leaveGroupDto,e);
            return HttpResult.fail(HttpErrorCodeEum.ERROR_ILLEGAL_ARGUMENT);
        }catch (Exception e){
            log.error("group/leave exception req={}",leaveGroupDto,e);
            return HttpResult.fail(-2,e.getMessage());
        }
        return HttpResult.success();
    }

    @PostMapping("/v1/group/dismiss")
    public HttpResult dismissGroup(@RequestBody DismissGroup dismissGroup){
        try {
            Preconditions.checkArgument(Objects.nonNull(dismissGroup.getGroupId()));
            Preconditions.checkArgument(Objects.nonNull(dismissGroup.getUserId()));
            groupManager.dismissGroup(dismissGroup);
        }catch (IllegalArgumentException e){
            log.error("group/dismiss dismissGroup=req={}",dismissGroup,e);
           return   HttpResult.fail(HttpErrorCodeEum.ERROR_ILLEGAL_ARGUMENT);
        }catch (Exception e){
            log.error("group/dismiss exception req={}",dismissGroup,e);
            return HttpResult.fail(-2,e.getMessage());
        }
        return HttpResult.success();
    }

    @PostMapping("/v1/group/edit/attribute")
    public HttpResult editGroupAttribute(@RequestBody EditGroupAttribute editGroupAttribute){
        try {
            Preconditions.checkArgument(Objects.nonNull(editGroupAttribute.getGroupId()));
            Preconditions.checkArgument(Objects.nonNull(editGroupAttribute.getUserId()));
            Preconditions.checkArgument(Objects.nonNull(Objects.nonNull(editGroupAttribute.getAttributeId())));
            Preconditions.checkArgument(Objects.nonNull(StringUtils.isNoneBlank(editGroupAttribute.getAttributeValue())));
            groupManager.editGroupAttribute(editGroupAttribute);
        }catch (IllegalArgumentException e){
            log.error("group/attribute editGroupAttribute=req={}",editGroupAttribute,e);
            return HttpResult.fail(HttpErrorCodeEum.ERROR_ILLEGAL_ARGUMENT);
        }catch (Exception e){
            log.error("group/attribute exception req={}",editGroupAttribute,e);
            return HttpResult.fail(-2,e.getMessage());
        }
        return HttpResult.success();
    }


    @GetMapping("/v1/group/members")
    public HttpResult getGroupMembers(@RequestParam Long userId,@RequestParam Long groupId){
        try {
            Preconditions.checkArgument(Objects.nonNull(userId));
            Preconditions.checkArgument(Objects.nonNull(groupId));
            final List<GroupMemberBo> groupMembers = groupManager.getGroupMembers(userId, groupId);
            return HttpResult.success(groupMembers);
        }catch (IllegalArgumentException e){
            log.error("group/members userId={},groupId={}",userId,groupId,e);
            return HttpResult.fail(HttpErrorCodeEum.ERROR_ILLEGAL_ARGUMENT);
        }catch (Exception e){
            log.error("group/members exception userId={},groupId={}",userId,groupId,e);
            return HttpResult.fail(-2,e.getMessage());
        }
    }

    @GetMapping("/v1/group/base/info")
    public HttpResult getGroupBaseInfo(@RequestParam Long userId,@RequestParam Long groupId){
        try {
            Preconditions.checkArgument(Objects.nonNull(userId));
            Preconditions.checkArgument(Objects.nonNull(groupId));
            final GroupBaseInfo groupBaseInfo = groupManager.getGroupBaseInfo(userId, groupId);
            return HttpResult.success(groupBaseInfo);
        }catch (IllegalArgumentException e){
            log.error("base/info userId={},groupId={}",userId,groupId,e);
            return HttpResult.fail(HttpErrorCodeEum.ERROR_ILLEGAL_ARGUMENT);
        }catch (Exception e){
            log.error("getGroupBaseInfo exception userId={},groupId={}",userId,groupId,e);
            return HttpResult.fail(-2,e.getMessage());
        }
    }

    @GetMapping("/v1/group/recommend")
    public HttpResult queryGroupRecommend(){
        try {
            final Long uid = UserContextHolder.getUserInfo().getUser().getUid();
            final List<GroupBaseInfo> groupBaseInfos = groupManager.getGroupRecommend(uid);
            return HttpResult.success(groupBaseInfos);
        }catch (IllegalArgumentException e){
            log.error("/v1/group/recommend ",e);
            return HttpResult.fail(HttpErrorCodeEum.ERROR_ILLEGAL_ARGUMENT);
        }catch (Exception e){
            log.error("queryGroupRecommend exception",e);
            return HttpResult.fail(-2,e.getMessage());
        }
    }

}
